/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.LoginBean;
import com.minierp.bean.UserStatus;
import com.minierp.dao.EnterpriseInfoDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.StafferDao;
import com.minierp.dao.TallySetDao;
import com.minierp.dao.UserDao;
import com.minierp.dao.UserPoDao;
import com.minierp.dao.UserPrivilegeDao;
import com.minierp.dao.WorkCenterDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncLoginFormCtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EnterpriseInfo;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginFormCtr extends SimpleFormController {
	private UserDao userDao;
	private EnterpriseInfoDao enterpriseInfoDao;
	private StafferDao stafferDao;
	private UserPrivilegeDao userPrivilegeDao;
	private TallySetDao tallySetDao;
	private UserPoDao userPoDao;
	private WorkCenterDao workCenterDao;
	private IdGenerator idGenerator;
	private DataSource dataSource;

	public UserPoDao getUserPoDao() {
		return this.userPoDao;
	}

	public void setUserPoDao(UserPoDao userPoDao) {
		this.userPoDao = userPoDao;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		EnterpriseInfo en = this.enterpriseInfoDao.get();
		EncCommonHelper.setEnterpriseInfo(request, en);
		EncLoginFormCtrHelper.parseLicenseInfo(request,
				this.getServletContext(), this.dataSource);
		return new LoginBean();
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		LoginBean bean = (LoginBean) command;
		bean.setRandomCode(EncCommonHelper.getCurrentRandomImageCode(request));
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		LoginBean bean = (LoginBean) command;
		UserStatus s = PageHelper.doUserLogin(bean.getUserName(),
				bean.getPassword(), "", this.userDao, this.stafferDao,
				this.userPoDao, this.userPrivilegeDao, this.workCenterDao,
				this.tallySetDao, this.idGenerator, request);
		String alertMsg = s.getAlertMsg();
		if (alertMsg.equals("")) {
			String map1 = request.getParameter("signonForwardUrl");
			if (map1 != null && !map1.equals("")) {
				response.sendRedirect(map1);
				return null;
			} else {
				ModelAndView mv = new ModelAndView(this.getSuccessView());
				return mv;
			}
		} else {
			HashMap map = new HashMap();
			map.put("command", bean);
			EncCommonHelper.setAlertMessage(request, alertMsg);
			return new ModelAndView(this.getFormView(), map);
		}
	}

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public EnterpriseInfoDao getEnterpriseInfoDao() {
		return this.enterpriseInfoDao;
	}

	public void setEnterpriseInfoDao(EnterpriseInfoDao enterpriseInfoDao) {
		this.enterpriseInfoDao = enterpriseInfoDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public UserPrivilegeDao getUserPrivilegeDao() {
		return this.userPrivilegeDao;
	}

	public void setUserPrivilegeDao(UserPrivilegeDao userPrivilegeDao) {
		this.userPrivilegeDao = userPrivilegeDao;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}
}